package com.example.httppost;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

/**
 * Http工具类
 * 
 * @author kevin.qiu
 *
 */
public class HttpUtils {

	/**
	 * 创建HttpClient
	 * 
	 * @return
	 */
	public static HttpClient makeHttpClient() {
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
		HttpConnectionParams.setSoTimeout(httpParams, 3000);
		HttpClient client = new DefaultHttpClient(httpParams);
		return client;
	}

	/**
	 * 获得返回的response
	 * 
	 * @param response
	 * @return 结果
	 */
	public static String getResponse(HttpResponse response) {

		String result = null;
		InputStream in = null;
		try {
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity);
				if (entity != null) {
					in = entity.getContent();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	/**
	 * 发送Post请求
	 * 
	 * @param client
	 * @param url
	 * @param paramsMap
	 * 
	 * @return 结果
	 */
	public static String post(HttpClient client, String url,
			Map<String, String> paramsMap) {
		String result = null;
		HttpPost post = new HttpPost(url);
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (Entry<String, String> entry : paramsMap.entrySet()) {
				params.add(new BasicNameValuePair(entry.getKey(), entry
						.getValue()));
			}
			post.setEntity(new UrlEncodedFormEntity(params));
			HttpResponse response = client.execute(post);
			result = HttpUtils.getResponse(response);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			post.abort();
		}

		return result;
	}

	/**
	 * 发送Get请求
	 * 
	 * @param client
	 * @param url
	 * 
	 * @return 结果
	 */
	public static String get(HttpClient client, String url) {
		String result = null;
		HttpGet get = new HttpGet(url);
		try {
			HttpResponse response = client.execute(get);
			result = HttpUtils.getResponse(response);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			get.abort();
		}

		return result;
	}

	// 调用demo
	 public static String testLogin(String userID,String userPassword ) {
	 // 制作httpClient
	 HttpClient client = HttpUtils.makeHttpClient();
	
	 // 制作url
	 String url = "http://localhost:8080/teachinglite/user/login";
	
	 // 制作post参数
	 Map<String, String> paramsMap = new HashMap<String, String>();
	 paramsMap.put("number", userID);
	 paramsMap.put("password", userPassword);
	
	 // 发送Post请求
	 String result = HttpUtils.post(client, url, paramsMap);
	 System.out.println(result);
	 return result;
	 }
}
